import {
  ServiceModel
} from '../../../../models/service'
const modelService = new ServiceModel()
const App =  getApp();

Page({
      /**
     * 页面的初始数据
     */
    data: {
      navs: ['全部', '待审核', '待寄回','已寄回','待退款','已退款','已拒绝'],
        winHeight:"",
        currentTab:0,
        scrollLeft: 0,
        pageIndex: 1,
        pageSize: 5,
        loading: false,
        hasNext: true,
        serviceList: [],
        empty:false,
        statusBarHeight: App.globalData.statusBarHeight,
        isTabbar:false
    },
  
    checkCor:function(){
      if (this.data.currentTab>4){
        this.setData({
          scrollLeft:300
        })
      }else{
        this.setData({
          scrollLeft:0
        })
      }
    },

  switchTab: function (e) {
      this.setData({
        currentTab: e.detail.current,
        pageIndex: 1,
        empty:false
      });
    this.checkCor();
    this.getServiceList();
    },

  swichNav: function (e) {
      var cur=e.currentTarget.dataset.current;
      if(this.data.currentTab==cur){return false;}
      else{
          this.setData({
            currentTab: cur,
            pageIndex: 1,
            empty:false
        })
          this.getServiceList();
      }
      },


  tapToDetail: function (event) {
    const returnId = event.currentTarget.dataset.returnid;
      wx.navigateTo({
        url: "/pages/account/service/detail/index?returnId=" + returnId
      })
    },
    
    //根据currentTab换算returnStatus
    formatStatus(currentTab) {
      switch (currentTab) {
        case 0:
          return 0;
          break;
        case 1:
          return 1;
          break;
        case 2:
          return 2;
          break;
        case 3:
          return 3;
          break;
        case 4:
          return 4;
          break;
        case 5:
          return 5;
          break;
      }
    },
    
    //获取售后列表
    getServiceList: function (e) {
      const self = this;
      self.setData({
        serviceList:[]
      })
      const params = {
        returnStatus: self.data.currentTab,
        pageIndex: self.data.pageIndex,
        pageSize: self.data.pageSize
      }

      modelService.getServiceList(params).then(res => {
        if (res.data.refundList.length == 0) {
          self.data.empty = true
        } else {
          self.data.empty = false
        }
        self.setData({
          empty:self.data.empty,
          serviceList: res.data.refundList,
          hasNext: res.data.hasNext,
          loading: false
        })
        wx.stopPullDownRefresh();
      }).catch();
    },
    
    //加载更多
    loadMore(isPull) {
      const self = this;  
      if (this.data.loading) {
        return
      }
      if (this.data.hasNext) {
        const params = {
          returnStatus: self.data.currentTab,
          pageIndex: self.data.pageIndex,
          pageSize: self.data.pageSize
        }
        if (isPull) {
          this.setData({
            loading: true
          })
        }
      
        modelService.getServiceList(params).then(res => {
          const tempArray = this.data.serviceList.concat(res.data.refundList)
          self.setData({
            serviceList: tempArray,
            hasNext: res.data.hasNext,
            loading: false
          })
        }).catch();
      }
    },
    
    //上滑到底
    lower: function () {
      this.data.pageIndex = this.data.pageIndex + 1
      this.loadMore(true)
    },
  
      /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that = this;
      wx.getSystemInfo({  
        success: function(res){  
          let clientHeight=res.windowHeight,
              clientWidth=res.windowWidth,
              rpxR=750/clientWidth;
          var calc=clientHeight*rpxR-86;
            that.setData( {  
                winHeight: calc  
            })
           }  
      })
      that.getServiceList();
      
    },
  
      /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
      const pages = getCurrentPages();
      const currPage = pages[pages.length - 1]; //当前页面
      const prevPage = pages[pages.length - 2]; //上一个页面
      if (prevPage.route == "pages/account/order/list/index"){
        prevPage.getOrderList();
      }
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function () {
      this.setData({
        pageIndex:1, 
        pageSize: 5
      })
      this.getServiceList();
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      
    },
})
